#pragma once

#include <memory>
#include "Protocol.hpp"

enum
{
    Success = 0,
    DivZeroErr,
    ModZeroErr,
    Unknown
};

class Calculator
{
public:
    Calculator()
    {}

    std::shared_ptr<Response> Cal(std::shared_ptr<Request> req)
    {
        std::shared_ptr<Response> resp = factory.BuildResponse();
        switch(req->GetOp())
        {
            case '+':
            {
                resp->SetResult(req->GetX()+req->GetY());
                break;
            }
            case '-':
            {
                resp->SetResult(req->GetX()-req->GetY());
                break;
            }
            case '*':
            {
                resp->SetResult(req->GetX()*req->GetY());
                break;
            }
            case '/':
            {
                if(req->GetY() == 0)
                {
                    resp->SetCode(DivZeroErr);
                }
                else
                {
                    resp->SetResult(req->GetX()/req->GetY());
                }
                break;
            }
            case '%':
            {
                if(req->GetY() == 0)
                {
                    resp->SetCode(ModZeroErr);
                }
                else
                {
                    resp->SetResult(req->GetX()%req->GetY());
                }
                break;
            }
            default:
            {
                resp->SetCode(Unknown);
                break;
            }
        }
        return resp;
    }

    ~Calculator(){}
public:
    Factory factory;
};